package chapter08

/**
 *
 * @author cdhuangchao3
 * @date 2023/6/11 4:53 PM
 */
object Test04_MatchObject {
  def main(args: Array[String]): Unit = {
    val student = new Student("zs", 18)

    // 针对对象实例内容进行匹配
    val result: String = student match {
      case Student("zs", 18) => "zs, 18"
      case _ => "Else"
    }
    println(result)
  }
}

class Student(val name: String, val age: Int)

object Student {
  def apply(name: String, age: Int) = new Student(name, age)

  // 必须实现一个unapply方法，用来对对象属性进行拆解
  def unapply(student: Student): Option[(String, Int)] = {
    if (student == null) {
      None
    } else {
      Some((student.name, student.age))
    }
  }
}